
const app = getApp()
const CatagoryHttp = app.CatagoryHttp()
const config = app.config()
const appkey = config.mapAppkey?config.mapAppkey:"3TXBZ-FZCCX-5IG4E-72TRA-YX4B7-XFFYG";

const scopeKeyMaps = {
  location:'scope.userLocation',
}


var QQMapWX = require('../../../utils/qqmap-wx-jssdk')
var qqmapsdk = new QQMapWX({
  key: appkey
});
// pages/market/market.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    background: ['1'],
    indicatorDots: false,
    vertical: false,
    autoplay: false,
    interval: 2000,
    duration: 500,
    grids: [],
    couponList: [0],
    padding: 20,
    showTopBtn: false,
    province: null,
    city: null,
    latitude: -1,
    longitude: -1,

    authscope:{},
    locationAuthorInfo:null
  },

  toggleLocation:function(){
    if(this.data.latitude==-1){
      this.relocation()
    }else{

    }
  },



  relocation: function () {
    const self = this;
    wx.getLocation({
      type: 'wgs84',
      success: function (res) {
        var latitude = res.latitude
        var longitude = res.longitude
        var speed = res.speed
        var accuracy = res.accuracy
        self.getGeoInfo(latitude, longitude)
        
      },
      fail:f=>this.setData({locationAuthorInfo:false})
    })
  },
  getSetting:function()
  {

  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: async function (options) {
    this.relocation()
    this.loadCatagory()
  },
  loadCatagory:async function(params) {
    // const res = await CatagoryHttp.loadList({categoryId:0}); 
    // if(res){
    //   this.setData({
    //     grids:res.result
    //   })
    // }
  },
  getGeoInfo: function (latitude, longitude) {
    let vm = this;
    qqmapsdk.reverseGeocoder({
      location: {
        latitude: latitude,
        longitude: longitude
      },
      success: function (res) {
        // debugger
        console.log(JSON.stringify(res));
        let province = res.result.ad_info.province
        let city = res.result.ad_info.city
        vm.setData({
          province: province,
          city: city,
          latitude: latitude,
          longitude: longitude,
        })

      },
      fail: function (res) {
        // debugger
        console.log(res);
      },
      complete: function (res) {
        // debugger
        // console.log(res);
      }
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow:async function () {
    const aaa = await this.getSettingAsync();
    const locationAuthorInfo = aaa[scopeKeyMaps.location]
    debugger
    if(this.data.latitude==-1){
      this.relocation()
    }
    this.setData({authscope:aaa,locationAuthorInfo})
  },

  settingPromise:function(){
    return new Promise((resolve,reject)=>{
      wx.getSetting({
        withSubscriptions: true,
        success:s=>resolve&&resolve(s.authSetting),
        fail:f=>resolve&&resolve({}),
      })
    })
  },

  getSettingAsync:async function(){
    const res = await this.settingPromise()
    return res
  },






  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    // debugger
    console.log('aaa')
    this.setData({
      couponList: this.data.couponList.concat(this.data.couponList)
    })
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  onPageScroll: function (e) {
  },
  bindtapTop: function (e) {
    wx.pageScrollTo({
      scrollTop: 0
    })
  },
  cellPressed: function (e) {
    debugger
    var data = e.currentTarget.dataset.data;
    var param = JSON.stringify(data);
    wx.navigateTo({
      url: '/pages/my/coupon/couponDetail?param='+param,
    })
  },
})